#include "gamepushbutton.h"
#include <QPixmap>
#include <QMouseEvent>
#include <QPropertyAnimation>

GamePushButton::GamePushButton(QWidget *parent) : QPushButton(parent)
{

}

GamePushButton::GamePushButton(QString imgNormal, QString imgPressed)
    : m_imgNormal(imgNormal),
      m_imgPressed(imgPressed)
{
    //
    if(m_imgNormal != "")
    {
        QPixmap normalPix;
        bool ret = normalPix.load(m_imgNormal);
        if (ret)
        {
            QSize pSize(normalPix.width(), normalPix.height());
            this->setFixedSize(pSize);
            this->setStyleSheet("QPushButton{border:0px}");
            this->setIcon(normalPix);
            this->setIconSize(pSize);
        }
    }
}

void GamePushButton::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton)
    {
        if(m_imgPressed != "")
        {
            QPixmap pressedPix;
            bool ret = pressedPix.load(m_imgPressed);
            if (ret)
            {
                QSize pSize(pressedPix.width(), pressedPix.height());
                this->setFixedSize(pSize);
                this->setStyleSheet("QPushButton{border:0px}");
                this->setIcon(pressedPix);
                this->setIconSize(pSize);
            }
        }
//        else
//        {
//            QPixmap normalPix;
//            bool ret = normalPix.load(m_imgNormal);
//            if (ret)
//            {
//                QSize pSize(normalPix.width() * 0.8, normalPix.height() *0.8);
//                normalPix.scaled(pSize);
//                this->setFixedSize(pSize);
//                this->setStyleSheet("QPushButton{border:0px}");
//                this->setIcon(normalPix);
//                this->setIconSize(pSize);
//            }
//        }
    }

    return QPushButton::mousePressEvent(e);
}

void GamePushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton)
    {
        if (m_imgNormal != "")
        {
            QPixmap normalPix;
            bool ret = normalPix.load(m_imgNormal);
            if (ret)
            {
                QSize pSize(normalPix.width(), normalPix.height());
                this->setFixedSize(pSize);
                this->setStyleSheet("QPushButton{border:0px}");
                this->setIcon(normalPix);
                this->setIconSize(pSize);
            }
        }
    }
    return QPushButton::mouseReleaseEvent(e);
}

void GamePushButton::zoomUp()
{
    QPropertyAnimation *upAnimation = new QPropertyAnimation(this, "geometry");
    upAnimation->setDuration(500);
    upAnimation->setStartValue(QRect(this->x(), this->y(), 100, 30));
    upAnimation->setEndValue(QRect(this->x(), this->y() + 10, 100, 30));
    upAnimation->setEasingCurve(QEasingCurve::OutBounce);

    upAnimation->start();
}

void GamePushButton::zoomDown()
{
    QPropertyAnimation *downAnimation = new QPropertyAnimation(this, "geometry");
    downAnimation->setDuration(500);
    downAnimation->setStartValue(QRect(this->x(), this->y() + 10, 100, 30));
    downAnimation->setEndValue(QRect(this->x(), this->y(), 100, 30));
    downAnimation->setEasingCurve(QEasingCurve::OutBounce);

    downAnimation->start();
}
